//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>
#include <ulinux/error.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "objs.h"
#include "wire/wl_callback.h"
#include "callbacks.h"
#include "wire/wl_display.h"
//------------------------------------------------------------------------------
k_u32 display_get_registry(void *registry_listener)
{
  k_u32 registry_id=objs_add(WL_REGISTRY,registry_listener);
  if(registry_id==OBJS_MAX){
    PERRC("FATAL:unable to add a registry object\n");
    sysc(exit_group,1,-1);
  }
  wl_display_req_get_registry(registry_id);
  return registry_id;
}

void display_sync(void (*done)(k_u32 serial))
{
  callbacks_listener.done=done;

  k_u32 callback_id=objs_add(WL_CALLBACK,&callbacks_listener);
  if(callback_id==OBJS_MAX){
    PERRC("FATAL:unable to add a callback object\n");
    sysc(exit_group,1,-1);
  }

  callbacks_id=callback_id;//record the current callback id

  wl_display_req_sync(callback_id);
}

static void ev_delete_id(k_u32 id)
{
  objs_del(id);
}

static void ev_error(k_u32 id,k_u32 code,k_u8 *str)
{
  PERR("FATAL:error received from the server, id=%u, code=%u, string=%s\n",id,
                                                                      code,str);
  sysc(exit_group,1,-1);
}

struct wl_display_listener display_listener={
  ev_error,//XXX:probably useless, even in the protocol
  ev_delete_id
};
